package com.letv.mail;

import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.letv.conf.ConfigFactory;

public class SimpleMailSender  {    
		private final static Logger log = LoggerFactory.getLogger(SimpleMailSender.class);

		private static SimpleMailSender instance;
	    /**
	     * 发送邮件的props文件
	     */
	    private final transient Properties props = System.getProperties();
	    /**
	     * 邮件服务器登录验证
	     */
	    private transient MailAuthenticator authenticator;
	 
	    /**
	     * 邮箱session
	     */
	    private transient Session session;


	    public static final SimpleMailSender getInstance() {
	    	synchronized(SimpleMailSender.class){
		        if (instance == null) {
		            try {
		                instance = new SimpleMailSender();
		                log.info("new SimpleMailSender().");
		            } catch (Exception e) {
		            	log.error("SimpleMailSender getInstance() error! ",e);
		            }
		        }
	    	}
	        return instance;
	    }
	
	    private SimpleMailSender(){
			String username = ConfigFactory.getString("mail.username");
			String password = ConfigFactory.getString("mail.password");
			String smtpHostName = ConfigFactory.getString("mail.stmthost");
		    init(username, password, smtpHostName);    	
	    }
	    
	    
/*	    *//**
	     * 初始化邮件发送器
	     * 
	     * @param username
	     *                发送邮件的用户名(地址)，并以此解析SMTP服务器地址
	     * @param password
	     *                发送邮件的密码
	     *//*
	    public SimpleMailSender(final String username, final String password) {
		    //通过邮箱地址解析出smtp服务器，对大多数邮箱都管用
		    final String smtpHostName = "smtp." + username.split("@")[1];
		    init(username, password, smtpHostName);
	 
	    }
	    
	    *//**
	     * 初始化邮件发送器
	     * 
	     * @param smtpHostName
	     *                SMTP邮件服务器地址
	     * @param username
	     *                发送邮件的用户名(地址)
	     * @param password
	     *                发送邮件的密码
	     *//*
	    public SimpleMailSender(final String smtpHostName, final String username,
	        final String password) {
	    	init(username, password, smtpHostName);
	    }*/
	 
	    /**
	     * 初始化
	     * 
	     * @param username
	     *                发送邮件的用户名(地址)
	     * @param password
	     *                密码
	     * @param smtpHostName
	     *                SMTP主机地址
	     */
	    private void init(String username, String password, String smtpHostName) {
		    // 初始化props
		    props.put("mail.smtp.auth", "true");
		    props.put("mail.smtp.host", smtpHostName);
		    // 验证
		    authenticator = new MailAuthenticator(username, password);
		    // 创建session
		    session = Session.getInstance(props, authenticator);
	    }
	 
	    /**
	     * 发送邮件
	     * 
	     * @param recipient
	     *                收件人邮箱地址
	     * @param subject
	     *                邮件主题
	     * @param content
	     *                邮件内容
	     * @throws AddressException
	     * @throws MessagingException
	     */
	    public void send(String recipient, String subject, Object content)
	        throws AddressException, MessagingException {
		    // 创建mime类型邮件
		    final MimeMessage message = new MimeMessage(session);
		    // 设置发信人
		    message.setFrom(new InternetAddress(authenticator.getUsername()));
		    // 设置收件人
		    message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
		    // 设置主题
		    message.setSubject(subject);
		    // 设置邮件内容
		    message.setContent(content.toString(), "text/html;charset=utf-8");
		    // 发送
		    Transport.send(message);
	    }
	 
	    /**
	     * 群发邮件
	     * 
	     * @param recipients
	     *                收件人们
	     * @param subject
	     *                主题
	     * @param content
	     *                内容
	     * @throws AddressException
	     * @throws MessagingException
	     */
	    public void send(List<String> recipients, String subject, Object content)
	        throws AddressException, MessagingException {
		    // 创建mime类型邮件
		    final MimeMessage message = new MimeMessage(session);
		    // 设置发信人
		    message.setFrom(new InternetAddress(authenticator.getUsername()));
		    // 设置收件人们
		    final int num = recipients.size();
		    InternetAddress[] addresses = new InternetAddress[num];
		    for (int i = 0; i < num; i++) {
		        addresses[i] = new InternetAddress(recipients.get(i));
		    }
		    message.setRecipients(RecipientType.TO, addresses);
		    // 设置主题
		    message.setSubject(subject);
		    // 设置邮件内容
		    message.setContent(content.toString(), "text/html;charset=utf-8");
		    // 发送
		    Transport.send(message);
	    }
	 
	    /**
	     * 发送邮件
	     * 
	     * @param recipient
	     *                收件人邮箱地址
	     * @param mail
	     *                邮件对象
	     * @throws AddressException
	     * @throws MessagingException
	     */
	    public void send(String recipient, MailSenderInfo mail)
	        throws AddressException, MessagingException {
	    	send(recipient, mail.getSubject(), mail.getContent());
	    }
	 
	    /**
	     * 群发邮件
	     * 
	     * @param recipients
	     *                收件人们
	     * @param mail
	     *                邮件对象
	     * @throws AddressException
	     * @throws MessagingException
	     */
	    public void send(List<String> recipients, MailSenderInfo mail)
	        throws AddressException, MessagingException {
	    	send(recipients, mail.getSubject(), mail.getContent());
	    }
	    
	    /**
	     * 群发邮件
	     * 
	     * @param mail
	     *                邮件对象
	     * @throws AddressException
	     * @throws MessagingException
	     */
	    public void send(MailSenderInfo mail)
	        throws AddressException, MessagingException {
	    	List<String> recipients = ConfigFactory.getList("mail.to");
	    	send(recipients, mail.getSubject(), mail.getContent());
	    }
	 

		    private static void testMail2(){
			      SimpleMailSender sms = SimpleMailSender.getInstance();   
			      
			      MailSenderInfo mailInfo = new MailSenderInfo();    
/*			      mailInfo.setMailServerHost("smtp.163.com");    
			      mailInfo.setMailServerPort("25");    
			      mailInfo.setValidate(true);    
			      mailInfo.setUserName("letvsearch@163.com");    
			      mailInfo.setPassword("letvsearch_123");//您的邮箱密码    
			      mailInfo.setFromAddress("letvsearch@163.com");    
			      mailInfo.setToAddress("gaojiawei@letv.com");   */ 
			      mailInfo.setSubject("新媒资系统运行监控邮件test");    
			      mailInfo.setContent("新媒资系统运行监控test：<h1>test</h1><table><tr><td>222</td></tr></table>");    
			      try {
					sms.send(mailInfo);
				} catch (AddressException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		    }
		    
		    public static void main(String[] args){   
				ConfigFactory.init(null);
		    	testMail2();
		    } 
		    
	}   
